package com.eqdd.yiqidian.ui.gongneng.renshi.zuzhiguanli;

import android.content.Intent;
import android.databinding.DataBindingUtil;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.PersonInfoActivityCustom;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.BaseBean;
import com.eqdd.yiqidian.bean.User;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpPresneter;
import com.eqdd.yiqidian.utils.EncryptUtil;
import com.eqdd.yiqidian.utils.GsonUtils;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.eqdd.yiqidian.utils.SPUtil;
import com.eqdd.yiqidian.utils.ToastUtil;

import android.view.View;

public class PersonInfoActivity extends BaseActivity {
    private PersonInfoActivityCustom dataBinding;
    private BaseBean<User> bean;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_person_info);
        initTopTitleBar(View.VISIBLE, "人员信息");
    }

    @Override
    public void initData() {
        String uid = getIntent().getStringExtra("uid");
        new HttpPresneter.Builder<String>(HttpConfig.LOGIN, String.class, PersonInfoActivity.this)

                .Params("uid", uid)
                .Params("u1", user.getUname())
                .Params("u2", EncryptUtil.sha1(SPUtil.getParam(Config.PASS, "") + "EQD"))
                .setOnHttpListener(new HttpPresneter.OnHttpListener<String>() {
                    @Override
                    public void onError(Throwable e) {
                        System.out.println(e.toString());
                        ToastUtil.showLong("不存在此帐号");
                        finish();
                    }

                    @Override
                    public void onNext(String o) {
                        bean = GsonUtils.changeGsonToBaseBean(o, User.class);
                        if (bean.getItems().size() > 0) {
                            updataView(bean.getItems().get(0));
                        } else {
                            ToastUtil.showLong("不存在此帐号");
                            finish();
                        }
                    }
                })
                .build()
                .post();
    }

    private void updataView(User user) {
        ImageUtil.setCircleImage(user.getIphoto(), dataBinding.ivPhoto);
        dataBinding.tvName.setText((user.getPname().equals("0") ? "游客" : user.getPname() + " " + (user.getSex() == 0 ? "男" : "女")));
    }

    @Override
    public void setView() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.bt_add:
                Intent intent = new Intent();
                System.out.println(dataBinding.tvName.getText().toString());
                intent.putExtra(Config.PERSON, bean.getItems().get(0));
                setResult(Config.SUCCESS, intent);
                finish();
                break;
        }
    }
}
